var content = document.querySelector('.content');
var tbody = document.querySelector(".grade-table tbody");
var searchBtn = document.getElementById('searchBtn');

content.addEventListener('mouseenter', function() {
  content.style.transition = 'background-color 0.3s';
  content.style.backgroundColor = 'rgba(100,100,100,0.5)'; // 将透明度更改为 0.5，使背景色加深
});

content.addEventListener('mouseleave', function() {
  content.style.transition = 'background-color 0.3s';
  content.style.backgroundColor = 'rgba(100,100,100,0.2)'; // 将透明度恢复为 0.2，使背景色变浅
});

var data = [];

$(document).keydown(function(event) {
  if (event.keyCode == 13) { // 13代表回车键的键码
    $('#searchBtn').click();
  }
});

document.addEventListener("DOMContentLoaded", function() {
  $.ajax({
      url: '/management/teaservlet/teaCheckCourse',                       //url方法接口：教师查询任课信息
      method: 'POST',
      data: {
        username: "1"
      },
      success: function(response) {
        console.log(response);
        console.log(response.data);
        data = response.data;
        // 将数据数组赋值给相应的元素
        var currentIndex = 1;
        data.forEach(function(item) {
          var row = document.createElement("tr");
          row.innerHTML = `
            <td>${currentIndex}</td>
            <td>${item.Cou_id}</td>
            <td>${item.Cou_name}</td>
            <td>${item.Cou_time}</td>
            <td>${item.Cou_location}</td>
          `;
          tbody.appendChild(row);
          currentIndex++;
        });
      }
    });

  var links = document.querySelectorAll('.sidebar ul li a');

  for (var i = 0; i < links.length; i++) {
    links[i].addEventListener('mouseenter', function() {
      this.style.transition = '0.2s';
      this.style.backgroundColor = '#679';
      this.style.borderRadius = '10px'; // 设置边缘的圆角半径为10px
    });

    links[i].addEventListener('mouseleave', function() {
      this.style.transition = '0.2s';
      this.style.backgroundColor = '';
      this.style.borderRadius = '10px'; // 鼠标移出时恢复原始边缘
    });
  }
});

searchBtn.addEventListener('click', function() {
      var searchInput = document.getElementById("search");
      var searchText = searchInput.value;
      $.ajax({
             url: '/management/teaservlet/searchCourse',                       //url方法接口：搜索
             method: 'POST',
             data: {
                  searchText: searchText
             },
             success: function(response) {
                console.log(response);
                var search = response.data;
                console.log(search);
                if(search.length == 0){
                    alert("没有符合要求的课程！");
                }
                else{
                tbody.innerHTML = "";
                currentIndex = 1;
                search.forEach(function(item) {
                          var row = document.createElement("tr");
                          row.innerHTML = `
                            <td>${currentIndex}</td>
                            <td>${item.Cou_id}</td>
                            <td>${item.Cou_name}</td>
                            <td>${item.Cou_time}</td>
                            <td>${item.Cou_location}</td>
                          `;
                          tbody.appendChild(row);
                          currentIndex++;
                        });
                }
             }
      });
});